﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Catering_DBOpertion;

namespace Catering_Operation
{
    public class RecipesRule
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.RecipesRule model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO RecipesRule(");
            strSql.Append("RecipesID,DTID,Repeatype,RepeatNo)");
            strSql.Append(" VALUES (");
            strSql.Append("@RecipesID,@DTID,@Repeatype,@RepeatNo)");
            SqlParameter[] parameters = {
					new SqlParameter("@RecipesID", SqlDbType.Int,4),
					new SqlParameter("@DTID", SqlDbType.Int,4),
					new SqlParameter("@Repeatype", SqlDbType.Int,4),
					new SqlParameter("@RepeatNo", SqlDbType.Int,4)};
            parameters[0].Value = model.RecipesID;
            parameters[1].Value = model.DTID;
            parameters[2].Value = model.Repeatype;
            parameters[3].Value = model.RepeatNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.RecipesRule model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE RecipesRule SET ");
            strSql.Append("RecipesID=@RecipesID,");
            strSql.Append("DTID=@DTID,");
            strSql.Append("Repeatype=@Repeatype,");
            strSql.Append("RepeatNo=@RepeatNo");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RecipesID", SqlDbType.Int,4),
					new SqlParameter("@DTID", SqlDbType.Int,4),
					new SqlParameter("@Repeatype", SqlDbType.Int,4),
					new SqlParameter("@RepeatNo", SqlDbType.Int,4),
                    new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.RecipesID;
            parameters[1].Value = model.DTID;
            parameters[2].Value = model.Repeatype;
            parameters[3].Value = model.RepeatNo;
            parameters[4].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM RecipesRule ");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteByRecipesID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM RecipesRule WHERE RecipesID=@ID");
            SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0 ? true : false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.RecipesRule GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 ID,RecipesID,DTID,Repeatype,RepeatNo FROM RecipesRule ");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.RecipesRule model = new Catering_Model.RecipesRule();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RecipesID"].ToString() != "")
                {
                    model.RecipesID = int.Parse(ds.Tables[0].Rows[0]["RecipesID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DTID"].ToString() != "")
                {
                    model.DTID = int.Parse(ds.Tables[0].Rows[0]["DTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Repeatype"].ToString() != "")
                {
                    model.Repeatype = int.Parse(ds.Tables[0].Rows[0]["Repeatype"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RepeatNo"].ToString() != "")
                {
                    model.RepeatNo = int.Parse(ds.Tables[0].Rows[0]["RepeatNo"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.RecipesRule GetModel(int RecipesID, int DTID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 ID,RecipesID,DTID,Repeatype,RepeatNo FROM RecipesRule ");
            strSql.Append(" WHERE RecipesID=@RecipesID AND DTID=@DTID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecipesID", SqlDbType.Int,4),
					new SqlParameter("@DTID", SqlDbType.Int,4)};
            parameters[0].Value = RecipesID;
            parameters[1].Value = DTID;

            Catering_Model.RecipesRule model = new Catering_Model.RecipesRule();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RecipesID"].ToString() != "")
                {
                    model.RecipesID = int.Parse(ds.Tables[0].Rows[0]["RecipesID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DTID"].ToString() != "")
                {
                    model.DTID = int.Parse(ds.Tables[0].Rows[0]["DTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Repeatype"].ToString() != "")
                {
                    model.Repeatype = int.Parse(ds.Tables[0].Rows[0]["Repeatype"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RepeatNo"].ToString() != "")
                {
                    model.RepeatNo = int.Parse(ds.Tables[0].Rows[0]["RepeatNo"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,RecipesID,DTID,Repeatype,RepeatNo FROM RecipesRule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }

    public class RecipesRuleManage
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.RecipesRuleManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO RecipesRuleManage( DTID,Repeatype,DefaultNo) VALUES (@DTID,@Repeatype,@DefaultNo)");
            SqlParameter[] parameters = {
                    new SqlParameter("@DTID", SqlDbType.Int,4),
                    new SqlParameter("@Repeatype", SqlDbType.Int,4),
					new SqlParameter("@DefaultNo", SqlDbType.Int,4)};
            parameters[0].Value = model.DTID;
            parameters[1].Value = model.Repeatype;
            parameters[2].Value = model.DefaultNo;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.RecipesRuleManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE RecipesRuleManage SET ");
            strSql.Append("Repeatype=@Repeatype,");
            strSql.Append("DefaultNo=@DefaultNo");
            strSql.Append(" WHERE DTID=@DTID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Repeatype", SqlDbType.Int,4),
                    new SqlParameter("@DefaultNo", SqlDbType.Int,4),
                    new SqlParameter("@DTID", SqlDbType.Int,4)};
            parameters[0].Value = model.Repeatype;
            parameters[1].Value = model.DefaultNo;
            parameters[2].Value = model.DTID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DTID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM RecipesRuleManage ");
            strSql.Append(" WHERE DTID=@DTID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DTID", SqlDbType.Int,4)};
            parameters[0].Value = DTID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.RecipesRuleManage GetModel(int DTID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 DTID,Repeatype,DefaultNo FROM RecipesRuleManage ");
            strSql.Append(" WHERE DTID=@DTID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DTID", SqlDbType.Int,4)};
            parameters[0].Value = DTID;

            Catering_Model.RecipesRuleManage model = new Catering_Model.RecipesRuleManage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DTID"].ToString() != "")
                {
                    model.DTID = int.Parse(ds.Tables[0].Rows[0]["DTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Repeatype"].ToString() != "")
                {
                    model.Repeatype = int.Parse(ds.Tables[0].Rows[0]["Repeatype"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DefaultNo"].ToString() != "")
                {
                    model.DefaultNo = int.Parse(ds.Tables[0].Rows[0]["DefaultNo"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT DTID,Repeatype,DefaultNo FROM RecipesRuleManage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
